<?php

class SearchController extends Zend_Controller_Action
{

    public function init()
    {
        try{
    		$session = Zend_Registry::get('session');
    		
    		if($session->user['Username'] != ""){ //ako ne e prazna sesijata znaci deka korisnikot e logiran.
    			
    		}        
        	else{        		
    			$this->_helper->redirector('index','login'); //vo sprotivno vrati go na stranata za logiranje.
    		} 
        
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";  
        
        } 
    }

    public function indexAction()
    {
    	try{
    		$session = Zend_Registry::get('session');
    		
    		$condition_BO = new Application_Model_DbTable_Conditions();
    		$threads_BO = new Application_Model_DbTable_Threads();
    		$friends_BO = new Application_Model_DbTable_Friends();
    		$groups_BO = new Application_Model_DbTable_Groups();
    		$flag_search = 0;
    		
    		if ($this->getRequest()->isPost()) {
    			if (isset($_POST['btn_SearchThreads'])){
    				
    				$thread_name =  $this->getRequest()->getParam("txt_SearchThreads");
    				$tag1 = $this->getRequest()->getParam("txt_SearchTag1");
    				$tag2 = $this->getRequest()->getParam("txt_SearchTag2");
    				$tag3 = $this->getRequest()->getParam("txt_SearchTag3");
    				$injectQuery = "AND (";
    				$subInject = "";
    				if ($tag1 != ""){
    					if ($subInject != "")
    					{
    						$subInject .= "OR ";
    					}
    					$subInject .=" Term ='" .$tag1. "'";
    				}
    				if ($tag2 != ""){
    				if ($subInject != "")
    					{
    						$subInject .= "OR ";
    					}
    					$subInject .=" Term ='" .$tag2. "'";
    				}
    				if ($tag3 != ""){
    				if ($subInject != "")
    					{
    						$subInject .= "OR ";
    					}
    					$subInject .=" Term ='" .$tag3. "'";
    				}
    				if ($subInject !=""){
    					$injectQuery .= $subInject . " )";
    				}
    				else
    				{
    					$injectQuery= "";
    				}
    				
    				$threads = $threads_BO->getSearchThreads($thread_name,$injectQuery);
    				$flag_search = 1;
    			}
    			elseif (isset($_POST['btn_SearchFriends'])){
    				
    				$friend_name =  $this->getRequest()->getParam("txt_SearchFriends");
    				$condition1 = $this->getRequest()->getParam("ddl_Conditions1");
    				$condition2 = $this->getRequest()->getParam("ddl_Conditions2");
    				$condition3 = $this->getRequest()->getParam("ddl_Conditions3");
    				$injectQuery = "AND (";
    				$subInject = "";
    				if ($condition1 != 0){
    					if ($subInject != "")
    					{
    						$subInject .= "OR ";
    					}
    					$subInject .=" Conditions.id  ='" .$condition1. "'";
    				}
    				if ($condition2 != 0){
    				if ($subInject != "")
    					{
    						$subInject .= "OR ";
    					}
    					$subInject .=" Conditions.id  ='" .$condition2. "'";
    				}
    				if ($condition3 != 0){
    				if ($subInject != "")
    					{
    						$subInject .= "OR ";
    					}
    					$subInject .=" Conditions.id ='" .$condition3. "'";
    				}
    				if ($subInject !=""){
    					$injectQuery .= $subInject . " )";
    				}
    				else
    				{
    					$injectQuery= "";
    				}
    				$userid= $session->user['id'];
    				$friends = $friends_BO->getSearchFriends($friend_name,$injectQuery,$userid);
    				$flag_search = 2;
    			}
    		elseif (isset($_POST['btn_SearchGroups'])){
    				
    				$group_name = $this->getRequest()->getParam("txt_SearchGroups");
    				   				
    				$groups = $groups_BO->getSearchGroups($group_name);
    				$flag_search = 3;
    			}
    		}
    		
    		$condtions=$condition_BO->getConditions();
    		    		
    		$this->view->condtions = $condtions;
	    	switch ($flag_search) {
			    case 1:
			    	$this->view->flag_search = $flag_search;
			        $this->view->found =$threads;
			        break;
			    case 2:
			        $this->view->flag_search = $flag_search;
			        $this->view->found =$friends;
			        break;
			    case 3:
			        $this->view->flag_search = $flag_search;
			        $this->view->found =$groups;
			        break;
			}
    		
    	}	
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }     	
    }

    public function matchmeAction()
    {
        try{
    		$session = Zend_Registry::get('session');
    		$friends_BO = new Application_Model_DbTable_Friends();
    		$matched = $friends_BO->matchMe($session->user['id']);
    		$this->view->matched = $matched;
    		
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }


}



